package com.topone.controller;

import com.topone.Enum.ConvenientInfoEnum;
import com.topone.Exception.FanZoneException;
import com.topone.Form.ConvenientInfoForm;
import com.topone.VO.ResultVO;
import com.topone.dataObject.ConvenientInfo;
import com.topone.service.ConvenientInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.Map;

/**
 * Created by Administrator on 2018/1/1,0001.
 */
@Controller
@RequestMapping("/seller/convenientInfo")
@Slf4j
public class SellerConvenientInfoController {
    @Autowired
    private ConvenientInfoService service;

    @GetMapping("/list")
    public ModelAndView list(@RequestParam(value="page",defaultValue = "1")   Integer page,
                             @RequestParam(value = "size",defaultValue = "10")   Integer size,
                             Map<String, Object> map){
        PageRequest request=new PageRequest(page-1,size);
        Page<ConvenientInfo> convenientInfoPage=service
                .findUpAll(ConvenientInfoEnum.OFF.getCode(),request);
        map.put("currentPage",page);
        map.put("size",size);
        map.put("convenientInfoPage",convenientInfoPage);
        return new ModelAndView("convenientInfo/list",map);
    }


    @GetMapping("/check")
    public ModelAndView check(@RequestParam("convenientId") String convenientId,
                              Map<String,Object> map){
        ConvenientInfo convenientInfo=service.findOne(convenientId);
        if(convenientInfo!=null){
            convenientInfo.setConvenientStatus(ConvenientInfoEnum.ON.getCode());
            try {
                service.save(convenientInfo);
                map.put("msg","审核通过");
                map.put("url","/fanZone/seller/convenientInfo/list");
            }catch (FanZoneException e){
                log.error("【便民信息审核】失败",e);
                map.put("msg","便民信息审核失败"+e);
                map.put("url","/fanZone/seller/convenientInfo/list");
                return new ModelAndView("common/error",map);
            }
        }
        return new ModelAndView("common/success",map);
    }

    @GetMapping("/close")
    public ModelAndView close(@RequestParam("convenientId") String convenientId,
                              Map<String,Object> map){
        ConvenientInfo convenientInfo=service.findOne(convenientId);
        if(convenientInfo!=null){
            convenientInfo.setConvenientStatus(ConvenientInfoEnum.OFF.getCode());
            try {
                service.save(convenientInfo);
                map.put("msg","信息下线");
                map.put("url","/fanZone/seller/convenientInfo/list");
            }catch (FanZoneException e){
                log.error("【便民信息下线】失败",e);
                map.put("msg","便民信息下线失败"+e);
                map.put("url","/fanZone/seller/convenientInfo/list");
                return new ModelAndView("common/error",map);
            }
        }
        return new ModelAndView("common/success",map);
    }

    @PostMapping("/save")
    public ModelAndView save(@Valid ConvenientInfoForm convenientInfoForm,
                             BindingResult bindingResult,
                             Map<String,Object> map){

        if(bindingResult.hasErrors()){
            log.error("【后台审核便民信息】参数不正确,convenientInfoForm={}",convenientInfoForm);
            map.put("msg",bindingResult.getFieldError().getDefaultMessage());
            map.put("url","/fanZone/seller/convenientInfo/list");
            return new ModelAndView("common/error",map);
        }


         return new ModelAndView("common/success", map);

    }
}
